# Maintainer: Jannick <thirdedition@gmx.net>

_realname=igraph
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.10.6
pkgrel=1
pkgdesc="Library for the analysis of networks (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url="https://igraph.org"
license=('spdx:GPL-2.0-or-later')
source=("https://github.com/igraph/igraph/releases/download/${pkgver}/igraph-${pkgver}.tar.gz"
        '0001-igraph-0.9.3-igraph.pc.in-add-member-Cflags.private.patch')
sha256sums=('99bf91ee90febeeb9a201f3e0c1d323c09214f0b5f37a4290dc3b63f52839d6d'
            '53e4ae45a640c10467ceaf939c78a3d27b87f1078db7555cf7acafa798a29c27')
depends=("${MINGW_PACKAGE_PREFIX}-glpk"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-arpack"))
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -b -i "${srcdir}/0001-igraph-0.9.3-igraph.pc.in-add-member-Cflags.private.patch"
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  for libtype in 'static' 'shared'; do
    case $libtype in
      (static) _BUILD_SHARED_LIBS=OFF;;
      (shared) _BUILD_SHARED_LIBS=ON;;
      (*) exit 1
    esac
    mkdir -p "${srcdir}"/build-${MSYSTEM}-${libtype} && cd "${srcdir}"/build-${MSYSTEM}-${libtype}
      MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      ${MINGW_PREFIX}/bin/cmake ../${_realname}-${pkgver} \
        -G 'Ninja' \
        -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
        -DBUILD_SHARED_LIBS=${_BUILD_SHARED_LIBS} \
        ${_extra_config[@]} \
        -DBUILD_TESTING=OFF \
        -DIGRAPH_ENABLE_TLS=ON \
        -DIGRAPH_GLPK_SUPPORT=ON \
        -DIGRAPH_GRAPHML_SUPPORT=ON \
        -DIGRAPH_WARNINGS_AS_ERRORS=OFF

      ${MINGW_PREFIX}/bin/cmake --build .
  done
}

check() {
  # igraph 0.9.4: All tests succeeded.
  for libtype in 'static' 'shared'; do
    ${MINGW_PREFIX}/bin/cmake ../${_realname}-${pkgver} -DBUILD_TESTING=ON 
    ${MINGW_PREFIX}/bin/cmake --build build-${MSYSTEM}-${libtype} --target build_tests
    ${MINGW_PREFIX}/bin/ctest --test-dir build-${MSYSTEM}-${libtype} --output-on-failure
  done
}

package() {
  # Install the libtypes one over another (until the simultaneous build of
  # shared and static libraries is implemented). :(
  for libtype in 'static' 'shared'; do
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}-${libtype}
  done

  # Remove path reference in cmake from libwebsockets
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/${_realname}/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -D -m644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
